package com.navinfo.parkingnavi.fragment;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mapbox.geojson.Feature;
import com.navinfo.parkingnavi.R;
import com.navinfo.parkingnavi.system.SystemConfig;
import com.navinfo.parkingnavi.util.GsonManager;
import com.navinf.parkingnavi.util.MapManager;
import com.navinfo.parkingnavi.util.PositionUtil;
import com.navinfo.parkingnavi.util.StringUtils;
import com.navinfo.parkingnavi.util.mode.Poi;

import java.io.Serializable;

/*
 *com.navinfo.parkingnavi.fragment
 *zhjch
 *2020/5/15
 *15:12
 *说明（poi详情页面）
 */
public class PoiInfoFragment extends BaseFragment implements View.OnClickListener {

    private TextView mName, mAddress;
    private View mCloseBtn, mLocBtn, mSearchNear, mNavi, mShare;
    private Feature mFeature;
    private Poi mPoi;


    public interface OnPoiInfoFragmentListener extends OnBaseFragmentListener {
        //点击关闭按钮
        void onClickClose();

        //点击周边搜索按钮
        void onClickSearch(Poi poi);

        //点击路线按钮
        void onClickNavi(Poi poi);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentView = inflater.inflate(R.layout.fragment_poi_info, null);

        mName = mFragmentView.findViewById(R.id.poi_info_name);
        mName.setOnClickListener(this);

        mAddress = mFragmentView.findViewById(R.id.poi_info_address);
        mAddress.setOnClickListener(this);

        mCloseBtn = mFragmentView.findViewById(R.id.poi_info_close);
        mCloseBtn.setOnClickListener(this);

        mLocBtn = mFragmentView.findViewById(R.id.poi_info_loc);
        mLocBtn.setOnClickListener(this);

        mSearchNear = mFragmentView.findViewById(R.id.poi_info_search);
        mSearchNear.setOnClickListener(this);

        mNavi = mFragmentView.findViewById(R.id.poi_info_navi);
        mNavi.setOnClickListener(this);

        mShare = mFragmentView.findViewById(R.id.poi_info_share);
        mShare.setOnClickListener(this);
        return mFragmentView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.poi_info_close:
                if (mListener != null && mListener instanceof OnPoiInfoFragmentListener) {
                    ((OnPoiInfoFragmentListener) mListener).onClickClose();
                }
                break;
            case R.id.poi_info_loc:
                MapManager.getInstance().jumpCurrentLoc();
                break;
            case R.id.poi_info_search:
                if (mListener != null && mListener instanceof OnPoiInfoFragmentListener) {
                    ((OnPoiInfoFragmentListener) mListener).onClickSearch(mPoi);
                }
                break;
            case R.id.poi_info_address:
            case R.id.poi_info_name:
                MapManager.getInstance().jumpPoi(mPoi);
                break;
            case R.id.poi_info_navi:
                if (mListener != null && mListener instanceof OnPoiInfoFragmentListener) {
                    ((OnPoiInfoFragmentListener) mListener).onClickNavi(mPoi);
                }
                break;
            case R.id.poi_info_share:
                Intent share_intent = new Intent();
                share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
                share_intent.setType("text/plain");//设置分享内容的类型
                share_intent.putExtra(Intent.EXTRA_SUBJECT, mPoi.getName_zh());//添加分享内容标题
                String url = "http://smap.navinfo.com/poi/share" +
                        "?Name_zh=" + mPoi.getName_zh() +
                        "&Province_name=" + mPoi.getProvince_name() +
                        "&lon=" + mPoi.getLon() +
                        "&lat=" + mPoi.getLat() +
                        "&Kindcode=" + mPoi.getKindcode() +
                        "&Cityadcode=" + mPoi.getCityadcode() +
                        "&Admincode=" + mPoi.getAdmincode() +
                        "&City_name=" + mPoi.getCity_name() +
                        "&Admin_name=" + mPoi.getAdmin_name();
                share_intent.putExtra(Intent.EXTRA_TEXT, url);//添加分享内容
                // 创建分享的Dialog
                share_intent = Intent.createChooser(share_intent, "分享poi");
                startActivity(share_intent);
                break;
        }
    }

    @Override
    public void onDataChange(Bundle bundle) {
        if (bundle != null) {
            Serializable serializable = bundle.getSerializable(SystemConfig.Bundle_key_poi_feature);
            if (serializable != null && serializable instanceof Feature) {
                mFeature = (Feature) serializable;
                mPoi = GsonManager.getInstance().parsePoi(mFeature);
                if (mPoi != null) {
                    mName.setText(mPoi.getName_zh());
                    String dis = "";
                    Location location = MapManager.getInstance().getMapBox().getLocationComponent().getLastKnownLocation();
                    if (location != null) {
                        double d = PositionUtil.getDistance(location.getLongitude(), location.getLatitude(), mPoi.getLon(), mPoi.getLat());
                        if (d > 1000) {
                            dis = StringUtils.formatDouble(d / 1000d, 2) + "km  ";
                        } else {
                            dis = StringUtils.formatDouble(d, 0) + "m  ";
                        }
                    }
                    mAddress.setText(dis + mPoi.getProvince_name() + "-" + mPoi.getCity_name() + mPoi.getAdmin_name());
                }
            }
        }
    }
}
